package com.example.springboot.controller.user;

import com.example.springboot.constant.ResConstansts;
import com.example.springboot.entity.Like;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.dto.LikeDto;
import com.example.springboot.service.LikeService;
import com.example.springboot.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user_likes")
public class LikeController {
    @Autowired
    private LikeService likeService;
    private JwtUtil jwtUtil;
    @PostMapping
    public Res<Void> follow(@RequestHeader(value = "Authorization", required = false) String token, @RequestBody LikeDto likeDto) {
        if (token == null)
            return new Res<Void>().code(401).msg("未登录");
        try {
            String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
            Integer likeId=jwtUtil.getUserIdByToken(reqToken);
            likeService.like(likeDto,likeId);
            return new Res<Void>().code(200).msg("操作成功");
        } catch (Exception e) {
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
    @GetMapping("/{likedId}")
    public Res<Integer> getIsLike(@RequestHeader(value = "Authorization", required = false) String token , @PathVariable Integer likedId) {
        if (token == null)
            return new Res<Integer>().code(401).msg("未登录").data(0);
        try {
            String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
            Integer likeId=jwtUtil.getUserIdByToken(reqToken);
            Like like=likeService.getIsLike(likedId,likeId);
            System.out.println(like);
            if(like==null || like.getIsLike()==0){
                return new Res<Integer>().code(200).msg("操作成功").data(0);
            }else{
                return new Res<Integer>().code(200).msg("操作成功").data(1);
            }
        } catch (Exception e) {
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
}
